package com.hundun.hddeal.service.lmpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hundun.hddeal.dao.NextOrderMapper;
import com.hundun.hddeal.dao.OrderDetailMapper;
import com.hundun.hddeal.dao.RecordMapper;
import com.hundun.hddeal.entity.NextOrder;
import com.hundun.hddeal.entity.NextOrderPuls;
import com.hundun.hddeal.entity.OrderDetail;
import com.hundun.hddeal.entity.Record;
import com.hundun.hddeal.response.Result;
import com.hundun.hddeal.response.ResultUtil;
import com.hundun.hddeal.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 记录表业务层
 * @author 谢
 */
@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    RecordMapper recordMapper;
    @Autowired
    NextOrderMapper nextOrderMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Override
    public Result deleteByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public Result insertSelective(Record record) {
        try {
            recordMapper.insertSelective(record);
            return ResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }

    }

    @Override
    public Result<Record> selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public Result updateByPrimaryKeySelective(Record record) {
        return null;
    }

    @Override
    public Result<PageInfo<Record>> selectAll(Integer page, Integer size, String user, Byte status, Date startTime,
                                              Date endTime, String content, String gzhName, String thisMonth, String lastMonth) {
        try {
            Double sum = 0.00;
            PageHelper.startPage(page, size);
            List<Record> list1 = recordMapper.selectAll(user, status, startTime, endTime, content, gzhName, thisMonth, lastMonth);
            for (Record record1 : list1) {
                if (status == 5) {
                    //判断是否有order跟orderId重复
                    int count = nextOrderMapper.orderCount(record1.getOrderId(), record1.getGzhName());
                    if (!(count > 1)) {
                        List<NextOrderPuls> nextOrderPulsList = nextOrderMapper.selectGetofTask
                                (null, record1.getOrderId(), null, null, null, null);
                        for (NextOrderPuls nextOrderPuls : nextOrderPulsList) {
                            NextOrder nextOrder = nextOrderMapper.selectByOreder(record1.getGzhName(), nextOrderPuls.getOrder(), count);
                            if (nextOrder != null) {
                                record1.setOutMoney(nextOrder.getOutMoney());
                                record1.setSettInfo(nextOrder.getSettInfo());
                            }
                        }

                    }
                    OrderDetail orderDetail = orderDetailMapper.selectByOrderId(record1.getOrderId());
                    if (orderDetail != null) {
                        record1.setOutMoney(orderDetail.getOutMoney());
                        record1.setSettInfo(orderDetail.getSettInfo());
                    }


                }

            }
            List<Record> list2 = recordMapper.selectAll(user, status, startTime, endTime, content, gzhName, thisMonth, lastMonth);
            for (Record record : list2) {
                sum += record.getMoney();
            }
            BigDecimal b = new BigDecimal(sum);
            PageInfo<Record> pageInfo = new PageInfo<>(list1);
            return ResultUtil.success(pageInfo, b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }

    }
}
